package com.controller;


import com.domain.*;
import com.service.MenuService;
import com.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;
    /*
     * 查询所有角色&&条件查询
     * */
    @RequestMapping("/findAllRole")
    public ResponseResult findAllRole(@RequestBody Role role){
        List<Role> roles = roleService.findAllRole(role);
        return new ResponseResult(true,200,"查询角色成功",roles);
    }
    /*
     * 添加及更新角色
     * */
    @RequestMapping("/saveOrUpdateRole")
    public ResponseResult saveOrUpdateRole(@RequestBody Role role){
        if (role.getId() == null){
            roleService.saveRole(role);
            return new ResponseResult(true,200,"添加成功",null);
        }else {
            roleService.updateRole(role);
            return new ResponseResult(true,200,"更新成功",null);
        }
    }

    @RequestMapping("/findAllMenu")
    public ResponseResult findAllMenu(){
        List<Menu> menus = menuService.findAllMenuByPid(-1);
        Map<String, Object> map = new HashMap<>();
        map.put("parentMenuList",menus);
        return new ResponseResult(true,200,"查询父子菜单成功",map);
    }
    /*
     * 根据角色ID查询关联的菜单id
     * */
    @RequestMapping("/findMenuByRoleId")
    public ResponseResult findMenuByRoleId(int roleId){
        List<Integer> list = roleService.findMenuByRoleId(roleId);
        return new ResponseResult(true,200,"查询角色关联菜单成功",list);
    }
    /*
     * 为角色分配菜单信息
     * */
    @RequestMapping("/RoleContextMenu")
    public ResponseResult RoleContextMenu(@RequestBody RoleMenuVO roleMenuVO){
        roleService.roleContextMenu(roleMenuVO);
        return new ResponseResult(true,200,"分配菜单成功",null);
    }
    /*
     * 删除角色
     * */
    @RequestMapping("/deleteRole")
    public ResponseResult deleteRole(int id){
        roleService.deleteRole(id);
        return new ResponseResult(true,200,"删除角色成功",null);
    }
    /*
     * 根据角色id查询对应的资源信息
     * */
    @RequestMapping("/findResourceListByRoleId")
    public ResponseResult findResourceListByRoleId(Integer roleId){
        List<ResourceCategory> categories = roleService.findResourceListByRoleId(roleId);
        return new ResponseResult(true,200,"资源信息返回成功",categories);
    }
    /*
     * 为角色分配资源信息
     * */
    @RequestMapping("/roleContextResource")
    public ResponseResult roleContextResource(@RequestBody RoleResourceVO roleResourceVO){
        roleService.roleContextResource(roleResourceVO);
        return new ResponseResult(true,200,"分配资源成功",null);
    }
}
